var users = [{Names: "张三",Numbs: "123456",Sex:"男",Places:"北京",Birs:"1993/3/12",address:"中国",Phone:"10086",Times:"2020/3/1",races:"水泥工",Depart:"宣传部",St:"在职"},
    {Names: "李四",Numbs: "126666",Sex:"男",Places:"北京",Birs:"1997/3/12",address:"中国",Phone:"10083",Times:"2020/10/1",races:"水泥工",Depart:"宣传部",St:"在职"},
    {Names: "刘鑫",Numbs: "111111",Sex:"女",Places:"常州",Birs:"1998/11/1",address:"中国",Phone:"10081",Times:"2020/5/3",races:"工程师",Depart:"企划部",St:"在职"},
    {Names: "李帅",Numbs: "124444",Sex:"女",Places:"上海",Birs:"2000/4/18",address:"中国",Phone:"10086",Times:"2020/7/14",races:"安全员",Depart:"企划部",St:"在职"},
    {Names: "张伟",Numbs: "156666",Sex:"男",Places:"北京",Birs:"1998/5/10",address:"中国",Phone:"10083",Times:"2020/8/23",races:"安全员",Depart:"宣传部",St:"在职"},
    {Names: "赵一",Numbs: "103333",Sex:"男",Places:"常州",Birs:"1993/9/7",address:"中国",Phone:"10082",Times:"2020/2/19",races:"安全员",Depart:"后勤部",St:"在职"}];
var NewSt = "在职";//在职状态
var exist = 0;// 0表示可进入存储
var Qexist = 0;//查询时判断
//var ChangeTemp = 0;//改变值时判断 0表示初次保存 1表示修改
var save = 0;//用于存储修改数组下标
var SaveRow ;//用于存储修改时，某一行的this
var Userline = 0;//记录用户原始数据
var different = 999;
function Save() {
    let table = document.getElementById("tableUserInfos");
    let worker_companyP = document.getElementById("companyP");
    let worker_Numb = document.getElementById("workerNumb");
    let worker_Name = document.getElementById("workerName");
    let sex = document.getElementById("sex");
    /*var sexselect = document.getElementById("sex");
    var index = sexselect.selectedIndex;
    sex = sexselect.options[index].value; //
    sexTxt = sexselect.options[index].text;*/
    let worker_Bir = document.getElementById("workerBir");
    let worker_Address = document.getElementById("workerAddress");
    let worker_Phone = document.getElementById("workerPhone");
    let worker_race = document.getElementById("race");
    let worker_depart = document.getElementById("depart");
    let worker_Time = document.getElementById("workerTime");
    if(worker_companyP.value === ""){
        alert("工地名称不能为空！");
        document.SetInfo.companyP.focus();
        return;
    }
    else if(worker_Numb.value === ""){
        alert("工人编号不能为空！");
        document.SetInfo.workerNumb.focus();
        return;
    }
    else if(worker_Name.value === ""){
        alert("工人名称不能为空！");
        document.SetInfo.workerName.focus();
        return;
    }
    else if(sex.value === ""){
        alert("性别不能为空！");
        document.SetInfo.sex.focus();
        return;
    }
    else if(worker_Bir.value === ""){
        alert("工人生日不能为空！");
        document.SetInfo.workerBir.focus();
        return;
    }
    else if(worker_Address.value === ""){
        alert("联系地址不能为空！");
        document.SetInfo.workerAddress.focus();
        return;
    }
    else if(worker_Phone.value === ""){
        alert("联系电话不能为空！");
        document.SetInfo.workerPhone.focus();
        return;
    }
    else if(worker_race.value === ""){
        alert("工种不能为空！");
        document.SetInfo.race.focus();
        return;
    }
    else if(worker_depart.value === ""){
        alert("职能不能为空！");
        document.SetInfo.depart.focus();
        return;
    }
    else if(worker_Time.value === ""){
        alert("注册时间不能为空！");
        document.SetInfo.workerTime.focus();
        return;
    }
    else if((worker_Numb.value.length !== 6) || !(/^\d+$/.test(worker_Numb.value) )){
        alert("工人编号不满足要求！");
        document.SetInfo.workerNumb.focus();
        return;
    }
    else if((worker_Phone.value.length !== 5) || !(/^\d+$/.test(worker_Numb.value) )){
        alert("工人手机号不满足要求！");
        document.SetInfo.workerPhone.focus();
        return;
    }
    let obj = {
        Names:worker_Name.value,
        Numbs:worker_Numb.value,
        Sex:sex.value,
        Places:worker_companyP.value,
        Birs:worker_Bir.value,
        address:worker_Address.value,
        Phone:worker_Phone.value,
        Times:worker_Time.value,
        races:worker_race.value,
        Depart:worker_depart.value,
        St:NewSt
    }

    for(let i in users){
        if(users[i].Numbs === worker_Numb.value) {//已存在编号
            exist = 1;
            confirm("编码已注册！请重新输入");
            break;
        }
        else {  //编号不存在
            exist = 0;
        }
    }
    if(exist === 0){
        users.push(obj);
        CloseWindow();
        show_result(users.length-1,table);
        //alert(users.length);
        //$("#white_content").load(location.href +"#white_content");
        //alert(123);

        /*$("#companyP").val(" ");
        $("#workerNumb").val(" ");
        $("#workerName").val(" ");
        $("#sex").val(" ");
        $("#workerBir").val(" ");
        $("#workerPhone").val(" ");
        $("#workerAddress").val(" ");
        $("#race").val(" ");
        $("#depart").val(" ");
        $("#workerTime").val(" ");*/
    }
    /*
          if(ChangeTemp === 1){ //修改
             exist = 0;
             //var numb = $(Info).closest("tr").find("td").eq(2).text();
             var Dochange = confirm("原编码："+ AllNumb + '\r' + "修改的编码：" + worker_Numb.value);
             if(Dochange){
                if(AllNumb === worker_Numb.value){//查询编码是否变化
                   for(let i in users){
                      if(worker_Numb.value === users[i].Numbs){
                         save = i;//记载下标
                      }
                   }
                   exist = 0;
                }
                else {
                   for(let i in users){
                      if(users[i].Numbs !== AllNumb && users[i].Numbs === worker_Numb.value){ //当修改的编码与数组其他编码冲突时
                         confirm("编码已注册！请重新输入");
                         save = i;//记载下标
                         exist = 1;
                      }
                      else{
                         save = i;//记载下标
                      }
                   }
                }
             }
             else {
                exist = 2;
                CloseWindow();
             }

          }
          else if (ChangeTemp === 0) { //若为新增
             for(let i in users){
                   if(users[i].Numbs === worker_Numb.value) {//已存在编号
                         exist = 1;
                         confirm("编码已注册！请重新输入");
                         break;
                      }
                   else {  //编号不存在
                   exist = 0;
                   }
             }
          }
             if(exist === 0){
                if(ChangeTemp === 0){
                   users.push(obj);
                   show_result(users.length-1,table);
                   //alert(users.length);
                   ChangeTemp = 0;
                   CloseWindow();
                }
                else {
                   users[save].Names = worker_Name.value;
                   users[save].Numbs = worker_Numb.value;
                   users[save].Places = worker_companyP.value;
                   users[save].Birs = worker_Bir.value;
                   users[save].Times = worker_Time.value;
                   users[save].races = worker_race.value;
                   users[save].Phone = worker_Phone.value;
                   users[save].address = worker_Address.value;
                   users[save].Sex = sex.value;
                   users[save].Depart = worker_depart.value;
                   users[save].St = NewSt;
                   //alert(11);
                   $(SaveRow).closest("tr").find("td").eq(1).text(worker_Name.value);
                   $(SaveRow).closest("tr").find("td").eq(2).text(worker_Numb.value);
                   $(SaveRow).closest("tr").find("td").eq(3).text(worker_companyP.value);
                   $(SaveRow).closest("tr").find("td").eq(4).text(worker_Bir.value);
                   $(SaveRow).closest("tr").find("td").eq(5).text(worker_Time.value);
                   $(SaveRow).closest("tr").find("td").eq(6).text(users[save].St.value);
                   //show_result(save,table);
                   //alert(12);
                   CloseWindow();
                   ChangeTemp = 0;
                   exist = 0;
                }
             }

    */


}
function Register() {
    //alert();

    let table = document.getElementById("tableUserInfos");
    let companyP22 = document.getElementById("companyP2");
    let Numb22 = document.getElementById("workerNumb2");
    let Name22 = document.getElementById("workerName2");
    let sex22 = document.getElementById("sex2");
    /*var sexselect = document.getElementById("sex");
    var index = sexselect.selectedIndex;
    sex = sexselect.options[index].value; //
    sexTxt = sexselect.options[index].text;*/
    let Bir22 = document.getElementById("workerBir2");
    let Address22 = document.getElementById("workerAddress2");
    let Phone22 = document.getElementById("workerPhone2");
    let race22 = document.getElementById("race2");
    let depart22 = document.getElementById("depart2");
    let Time22 = document.getElementById("workerTime2");
    if(companyP22.value === ""){
        alert("工地名称不能为空！");
        document.SetInfo.companyP.focus();
        return;
    }
    else if(Numb22.value === ""){
        alert("工人编号不能为空！");
        document.SetInfo.workerNumb.focus();
        return;
    }
    else if(Name22.value === ""){
        alert("工人名称不能为空！");
        document.SetInfo.workerName.focus();
        return;
    }
    else if(sex22.value === ""){
        alert("性别不能为空！");
        document.SetInfo.sex.focus();
        return;
    }
    else if(Bir22.value === ""){
        alert("工人生日不能为空！");
        document.SetInfo.workerBir.focus();
        return;
    }
    else if(Address22.value === ""){
        alert("联系地址不能为空！");
        document.SetInfo.workerAddress.focus();
        return;
    }
    else if(Phone22.value === ""){
        alert("联系电话不能为空！");
        document.SetInfo.workerPhone.focus();
        return;
    }
    else if(race22.value === ""){
        alert("工种不能为空！");
        document.SetInfo.race.focus();
        return;
    }
    else if(depart22.value === ""){
        alert("职能不能为空！");
        document.SetInfo.depart.focus();
        return;
    }
    else if(Time22.value === ""){
        alert("注册时间不能为空！");
        document.SetInfo.workerTime.focus();
        return;
    }
    else if((Numb22.value.length !== 6) || !(/^\d+$/.test(Numb22.value) )){
        alert("工人编号不满足要求！");
        document.SetInfo.workerNumb.focus();
        return;
    }
    else if((Phone22.value.length !== 5) || !(/^\d+$/.test(Phone22.value) )){
        alert("工人手机号不满足要求！");
        document.SetInfo.workerPhone.focus();
        return;
    }
    let obj = {
        Names:Name22.value,
        Numbs:Numb22.value,
        Sex:sex22.value,
        Places:companyP22.value,
        Birs:Bir22.value,
        address:Address22.value,
        Phone:Phone22.value,
        Times:Time22.value,
        races:race22.value,
        Depart:depart22.value,
        St:NewSt
    }
    //var numb = $(Info).closest("tr").find("td").eq(2).text();
    var Dochange = confirm("原编码："+ AllNumb + '\r' + "修改的编码：" + Numb22.value);
    if(Dochange){
        if(AllNumb === Numb22.value){//查询编码是否变化
            for(let i in users){
                if(Numb22.value === users[i].Numbs){
                    save = i;//记载下标
                }
            }
        }
        else {
            for(let i in users){
                if(users[i].Numbs !== AllNumb && users[i].Numbs === Numb22.value){ //当修改的编码与数组其他编码冲突时
                    confirm("编码已注册！请重新输入");
                    save = i;//记载下标
                    exist = 1;
                }
                else{
                    save = i;//记载下标
                }
            }
        }
    }
    else {
        exist = 2;
        CloseWindow2();
    }
    CloseWindow2();
    users[save].Names = Name22.value;
    users[save].Numbs = Numb22.value;
    users[save].Places = companyP22.value;
    users[save].Birs = Bir22.value;
    users[save].Times = Time22.value;
    users[save].races = race22.value;
    users[save].Phone = Phone22.value;
    users[save].address = Address22.value;
    users[save].Sex = sex22.value;
    users[save].Depart = depart22.value;
    users[save].St = NewSt;
    //alert(11);
    $(SaveRow).closest("tr").find("td").eq(1).text(Name22.value);
    $(SaveRow).closest("tr").find("td").eq(2).text(Numb22.value);
    $(SaveRow).closest("tr").find("td").eq(3).text(companyP22.value);
    $(SaveRow).closest("tr").find("td").eq(4).text(Bir22.value);
    $(SaveRow).closest("tr").find("td").eq(5).text(Time22.value);
    $(SaveRow).closest("tr").find("td").eq(6).text(users[save].St.value);
    //show_result(save,table);
    //alert(12);
    //CloseWindow();
    ChangeTemp = 0;
    exist = 0;

}
function Query(){ //查询

    var constrN = document.Info.constrN.value;
    var workerN = document.Info.workerN.value;

    if(constrN === ""){
        alert("工地名称不能为空！");
        document.Info.constrN.focus();
        return;
    }
    else if (workerN === ""){
        alert("工人名称不能为空！");
        document.Info.workerN.focus();
        return;
    }
    //alert(users.length);
    for (var i in users) {
        if((users[i].Places === constrN) && (users[i].Names === workerN)) {
            var table = document.getElementById("tableUserQuery")
            show_result(i, table);
            Qexist = 1;
            break;
        }
        else{
            Qexist = 0;
        }
    }
    if(Qexist === 0){
        alert("查询失败,用户不存在或数据错误！");
    }

}
function AddWindow() {//打开窗口
    //alert(1);
    //newWindows = window.open("Register.html","new","width = 600,height = 700,top = 300,left = 100");
    document.getElementById("Second").style.display = 'block';
    document.getElementById('fade').style.display='block';
    document.getElementById('myCanvas').style.display = 'block';
}
function CloseWindow() {//关闭窗口
    //alert(1);
    document.getElementById("Second").style.display='none';
    document.getElementById('fade').style.display='none';
}
function AddWindow5() {//打开窗口
    //alert(2);
    document.getElementById("Third").style.display = 'block';
    document.getElementById('fade2').style.display='block';
    //document.getElementById('myCanvas').style.display = 'block';
}
function CloseWindow2() {//关闭窗口
    document.getElementById('Third').style.display='none';
    document.getElementById('fade2').style.display='none';
}

function fill() { //填充默认
    var table=document.getElementById("tableUserInfos")
    for (var i in users) {
        show_result(i,table);

    }
}
function Infochange(Info) { //修改按钮响应事件
    ChangeTemp = 1; //1表示修改 0表示注册
    SaveRow = Info;  //记录修改行this
    //alert($(Info).closest("tr").find("td").eq(2).text());//获取编号
    AddWindow5();  //打开注册/修改div
    AllNumb = $(Info).closest("tr").find("td").eq(2).text(); //记录当前行的编码
    for (var i in users){
        if(users[i].Numbs === AllNumb){   //在数组中找到该编码的所有信息并显示在 注册/修改div中
            Userline = i;
            $("#companyP2").val(users[i].Places);
            $("#workerNumb2").val(users[i].Numbs);
            $("#workerName2").val(users[i].Names);
            $("#sex2").val(users[i].Sex);
            $("#workerBir2").val(users[i].Birs);
            $("#workerPhone2").val(users[i].Phone);
            $("#workerAddress2").val(users[i].address);
            $("#race2").val(users[i].races);
            $("#depart2").val(users[i].Depart);
            $("#workerTime2").val(users[i].Times);
        }
    }
    //CloseWindow2();
    //alert();
}
function Delete1(Row) {
    if(confirm("是否删除？") === true){
        // 获取表格
        var LineNumb1 = $(Row).closest("tr").find("td").eq(2).text();//获取编码
        for (var i in users){
            if(users[i].Numbs === LineNumb1) {   //在数组中找到该编码的所有信息并显示在 注册/修改div中
                users[i].St = "离职";
            }
        }
        let table1 = document.getElementById("tableUserInfos");
        // 根据this返回值删除当前行
        table1.deleteRow(Row.parentNode.parentNode.rowIndex);
    }
}
function Delete2(Row) {
    if(confirm("是否删除？") === true){
        var LineNumb2 = $(Row).closest("tr").find("td").eq(2).text();//获取编码
        for (var i in users){
            if(users[i].Numbs === LineNumb2) {   //在数组中找到该编码的所有信息并显示在 注册/修改div中
                users[i].St = "离职";
            }
        }
        // 获取表格
        let table2 = document.getElementById("tableUserQuery");
        // 根据this返回值删除当前行
        table2.deleteRow(Row.parentNode.parentNode.rowIndex);
    }
}
function SelectAll(e) {
    var all = document.getElementById("select_all");
    var selects = document.getElementsByName("select");
    for(var i=0;i<selects.length;i++){
        selects[i].checked = all.checked;
    }
}
function NewSelectAll(e) {
    var all = document.getElementById("newselect_all");
    var selects = document.getElementsByName("select");
    for (var i = 0; i < selects.length; i++) {
        selects[i].checked = all.checked;
    }
}
function show_result(a,table) {
    let newRow = table.insertRow();
    rows = table.rows.length;
    let cellselect = newRow.insertCell();
    let cellName = newRow.insertCell();
    let cellcode = newRow.insertCell();
    let cellwplace = newRow.insertCell();
    let cellDate = newRow.insertCell();
    let cellTime = newRow.insertCell();
    let cellNow = newRow.insertCell();
    let cellChange = newRow.insertCell();
    cellselect.innerHTML = "<input type='checkbox' name='select'>";
    cellName.innerHTML = users[a].Names;
    cellcode.innerHTML = users[a].Numbs;
    cellwplace.innerHTML = users[a].Places;
    cellDate.innerHTML = users[a].Birs;
    cellTime.innerHTML = users[a].Times;
    cellNow.innerHTML = users[a].St;
    if(table === tableUserInfos){
        cellChange.innerHTML = "<input type='button' value='修改' style='color: white;width: 70px' id='change' onclick='Infochange(this)'>" +
            "<input type='button' style='color: white;width: 70px' id = 'delete' value='删除' onclick='Delete1(this);'>";
    }
    else if(table === tableUserQuery){
        cellChange.innerHTML = "<input type='button' value='修改' style='color: white;width: 70px' id='change' onclick='Infochange(this)'>" +
            "<input type='button' style='color: white;width: 70px' id =  'delete' value='删除' onclick='Delete2(this);'>";
    }

}
function Glance() {
    PFile = document.getElementById("PFile");
    PreView = document.getElementById("avatarPreview");
    PreView.src = $('#PFile')[0];
}
